package org.xwalk.core;

import android.util.Log;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.security.Principal;
import java.security.PrivateKey;
import java.util.List;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;



public class ClientCertRequestHandler   implements ClientCertRequest  {

    private ArrayList<Object> constructorTypes;
    private ArrayList<Object> constructorParams;
    private ReflectMethod postWrapperMethod;


    private XWalkCoreWrapper coreWrapper;
    private Object bridge;

    protected Object getBridge() {
        return bridge;
    }

    public ClientCertRequestHandler(Object bridge) {
        this.bridge = bridge;
        reflectionInit();
    }


    public void proceed(final PrivateKey privateKey, final List<X509Certificate> chain) {
        try {
            proceedPrivateKeyListMethod.invoke(privateKey, chain);
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        
    }

    private ReflectMethod proceedPrivateKeyListMethod = new ReflectMethod(null, "proceed");


    public void ignore() {
        try {
            ignoreMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        
    }

    private ReflectMethod ignoreMethod = new ReflectMethod(null, "ignore");


    public void cancel() {
        try {
            cancelMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        
    }

    private ReflectMethod cancelMethod = new ReflectMethod(null, "cancel");


    public String getHost() {
        try {
            return (String)getHostMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getHostMethod = new ReflectMethod(null, "getHost");


    public int getPort() {
        try {
            return (Integer)getPortMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return 0;
    }

    private ReflectMethod getPortMethod = new ReflectMethod(null, "getPort");


    public String[] getKeyTypes() {
        try {
            return (String[])getKeyTypesMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getKeyTypesMethod = new ReflectMethod(null, "getKeyTypes");


    public Principal[] getPrincipals() {
        try {
            return (Principal[])getPrincipalsMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getPrincipalsMethod = new ReflectMethod(null, "getPrincipals");


    void reflectionInit() {
        XWalkCoreWrapper.initEmbeddedMode();

        coreWrapper = XWalkCoreWrapper.getInstance();
        if (coreWrapper == null) {
            XWalkCoreWrapper.reserveReflectObject(this);
            return;
        }

        proceedPrivateKeyListMethod.init(bridge, null,
                "proceedSuper", PrivateKey.class, List.class);
        ignoreMethod.init(bridge, null,
                "ignoreSuper");
        cancelMethod.init(bridge, null,
                "cancelSuper");
        getHostMethod.init(bridge, null,
                "getHostSuper");
        getPortMethod.init(bridge, null,
                "getPortSuper");
        getKeyTypesMethod.init(bridge, null,
                "getKeyTypesSuper");
        getPrincipalsMethod.init(bridge, null,
                "getPrincipalsSuper");
    }

}
